// base
import Taro, { Component } from '@tarojs/taro'
import { View, Image, ScrollView } from '@tarojs/components'
// less
import './index.less'

import { screenCheckMark } from '../../../configuration/staticimg'
import ScreenCardItems from '../screen-card-items'
import { navigateTo } from '../../../crossplatform/apiservice/navigate'
import pagejumplist from '../../../configuration/pagejumplist.json'

class ScreenList extends Component {
  static defaultProps = {
    dataSource: []
  }
  componentWillMount() {
    // onLoad
  }

  componentDidMount() {
    // onready
  }

  componentWillUnmount() {
    // onUnload
  }

  componentWillReceiveProps(nextprops) {}

  clickOrder = e => {
    let index = e.currentTarget.dataset.index
    const { screenIndex } = this.props
    this.props.onTap({ index, screenIndex })
  }

  onClickCardItem = (titleIndex, cardIndex) => {
    const { screenIndex } = this.props
    this.props.onTap({ screenIndex: screenIndex, titleIndex, cardIndex })
  }

  onClickCardArea = cardIndex => {
    const { screenIndex } = this.props
    this.props.onTap({ screenIndex: screenIndex, cardIndex })
  }

  onPostJob = () => {
    // 跳转到发布岗位页面
    const { barlevecurrent } = this.props
    let pagetype = ''
    switch (barlevecurrent) {
      case 0:
        pagetype = 'parttime'
        break
      case 1:
        pagetype = 'practime'
        break
      case 2:
        pagetype = 'fulltime'
        break
      default:
        pagetype = 'parttime'
        break
    }
    navigateTo({
      url: `${pagejumplist['manage-jobs'].path}?pagetype=${pagetype}`
    })
  }

  render() {
    const { dataSource, type, showTitle, role, pagetype } = this.props
    return (
      <View className="ScreenList_id84552">
        {type === 'list' ? (
          <View className="screen-list">
            {dataSource.map((item, index) => {
              return (
                <View
                  key={index}
                  className={`screen-list-item ${item.active ? 'active' : ''}`}
                  onClick={this.clickOrder}
                  data-index={index}
                >
                  {item.name}
                  <Image src={screenCheckMark} />
                </View>
              )
            })}
          </View>
        ) : (
          <ScreenCardItems
            role={role}
            showTitle={showTitle}
            data={dataSource}
            pagetype={pagetype}
            onTap={this.onClickCardItem}
            onTapArea={this.onClickCardArea}
            onPostJob={this.onPostJob}
          />
        )}
      </View>
    )
  }
}

export default ScreenList
